package me.sdau.spider.openapi.web;

import me.sdau.spider.openapi.comm.aop.LoggerManage;
import me.sdau.spider.openapi.comm.utils.SystemUtil;
import me.sdau.spider.openapi.dto.Result;
import me.sdau.spider.openapi.exception.AuthException;
import me.sdau.spider.openapi.service.ElasticSearchService;
import org.elasticsearch.action.get.GetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class ElasticSearchContraller {

    @Autowired
    private ElasticSearchService elasticSearchService;

    @GetMapping("/{index}/{type}/{id}")
    @LoggerManage(description = "根据 ID 查找")
    public ResponseEntity<Map<String, Object>> getById(@PathVariable("index") String index,
                                                       @PathVariable("type") String type,
                                                       @PathVariable("id") String id) {
        List<String> apis = SystemUtil.getApis();
        String api = "/" + index + "/" + type;
        if (!apis.contains(api)) {
            throw new AuthException("权限不足,path=[" + api + "]");
        }
        GetResponse response = elasticSearchService.getById(index, type, id);
        return new ResponseEntity<>(response.getSource(), HttpStatus.OK);
    }

    /**
     * 示例 http://localhost:8080/api/xrs_db/meitan__news?start=2018-03-23&end=2018-03-26&query={"field":["source","title"],"text":"煤炭 + 中国"}
     *
     * @param index     库名
     * @param type      表名
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param queryStr  简单查询的关键词
     * @return
     */
    @GetMapping("/{index}/{type}")
    @LoggerManage(description = "全文搜索 DOC ")
    public ResponseEntity<Result> queryByTime(@PathVariable("index") String index,
                                              @PathVariable("type") String type,
                                              @RequestParam(value = "start", required = false) String startTime,
                                              @RequestParam(value = "end", required = false) String endTime,
                                              @RequestParam(value = "query", required = false) String queryStr) {
        // 权限校验
//        List<String> apis = SystemUtil.getApis();
//        String api = "/" + index + "/" + type;
//        if (!apis.contains(api)) {
//            throw new AuthException("权限不足,path=[" + api + "]");
//        }

        List<Map<String, Object>> list = elasticSearchService.queryBytime(index, type, startTime, endTime, queryStr);
        return new ResponseEntity<>(Result.of(list), HttpStatus.OK);
    }
}
